/*
 * Created on 2007-8-28
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.tongji.quickCRUDQ.define;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import com.tongji.quickCRUDQ.util.StringUtil;


/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Row implements Serializable {
  private List columns=new ArrayList();
  private List distinctList=new ArrayList();
  
  public void addColumnList(Column[] array){
  	if(array==null) return;
  	for (int i = 0; i < array.length; i++) {
		Column column = array[i];
	  	this.columns.add(column);
	}
  }
  
  public void addColumnList(List columns){
  	this.columns.addAll(columns);
  }
  
  public void addColumn(Column column){
  	if(column==null) return;
  	this.columns.add(column);
  }
  
//  public void addTableColumns(String tableName){
//  	ColumnDao dao=DaoUtil.getColumnDao();
//  	List columns=dao.getColumnList(tableName);
//  	this.columns.addAll(columns);
//  }
  
  public List getColumnList(){
  	return order(this.columns);
  }
  

private List order(List list) {
	Comparator comp=new ColumnCompare();
  	Collections.sort(list,comp);
  	return list;
}

public List getDistinctColumnList(){
  	distinctList.clear();
  	int size=this.columns.size();
  	for (int i = 0; i < size; i++) {
//  		System.out.println(columns.get(i));
		Column column=(Column)columns.get(i);
		if(distinctList.contains(column)==false) distinctList.add(column);
	}
  	return order(distinctList);
  }

public void removeColumn(String columnName,String tableName){
	columnName=StringUtil.NVL(columnName,"");
	tableName=StringUtil.NVL(tableName,"");
	List columns=this.getColumnList();
	List tempColumns=new ArrayList();
	for (Iterator iter = columns.iterator(); iter.hasNext();) {
		Column column = (Column) iter.next();
		String cname=column.getColumnname();
		Table table=column.getTable();
		String tname=table==null?"":table.getTablename();
		if(cname.equals(columnName)&&tableName.equals(tname)){
			tempColumns.add(column);
		}
	}
	columns.removeAll(tempColumns);
    
}

public Column findColumn(String columnName,String tableName){
	columnName=StringUtil.NVL(columnName,"");
	tableName=StringUtil.NVL(tableName,"");
	List columns=this.getDistinctColumnList();
	for (Iterator iter = columns.iterator(); iter.hasNext();) {
		Column column = (Column) iter.next();
		String cname=column.getColumnname();
		Table table=column.getTable();
		String tname=table==null?"":table.getTablename();
		if(cname.equals(columnName)&&tableName.equals(tname)) return column;
	}
	return null;
}
  
  class ColumnCompare implements Comparator{

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object obj1, Object obj2) {
        Column column1,column2;
        int k1,k2;
		column1=(Column)obj1;
		column2=(Column)obj2;
		k1=column1.getIndx();
		k2=column2.getIndx();
		if(k1==k2)	return 0; else if(k1<k2) return -1; else return 1;
	}
  	
  }
 
}
